﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAT.Model;
using System.Windows.Forms;


namespace MAT.Controllers
{
    class ctrAssignment
    {
        private frmAssignment _frmAssignment = null;

        public frmAssignment FrmAssignment
        {
            get { return _frmAssignment; }
            set { _frmAssignment = value; }
        }
        MAT_Context context = new MAT_Context();
        public void frmAssignment_Load()
        {
            List<ROOM> Rooms = context.ROOMs.ToList();
            _frmAssignment.cmbRoom.DisplayMember = "Room_name";
            _frmAssignment.cmbRoom.ValueMember = "Room_ID";
            _frmAssignment.cmbRoom.DataSource = Rooms;
        }
        public void cmbRoom_SelectedIndexChanged()
        {
            _frmAssignment.cmbEmployee.DataSource = null;

            ROOM room = context.ROOMs.FirstOrDefault(r => r.ROOM_ID == (string)_frmAssignment.cmbRoom.SelectedValue);
            if (room != null)
            {
                List<OWNER> owner = room.OWNERS.ToList();
                _frmAssignment.cmbEmployee.DataSource = owner;
                _frmAssignment.cmbEmployee.DisplayMember = "employee_name";
                _frmAssignment.cmbEmployee.ValueMember = "employee_id";

            }
        }
        public void btnClose_Click()
        {
            _frmAssignment.Close();
        }
        public void btnAssignment_Click()
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure you want to update this asset?", "Update Asset", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            string asset_id = _frmAssignment.txtAsset.Text;
            ASSET asset = context.ASSETs.FirstOrDefault(ca => ca.ASSET_ID == asset_id);
            if (asset != null)
            {
                asset.EMPLOYEE_ID = _frmAssignment.cmbEmployee.SelectedValue.ToString();
                if (result == DialogResult.OK)
                {
                    context.SaveChanges();
                    MessageBox.Show("Successful!");

                }
            }
        }

    }
}
